package com.nanjing.east.cloud.service.impl;

import com.nanjing.east.cloud.dao.PayHardwareDAO;
import com.nanjing.east.cloud.dao.PayOrderDAO;
import com.nanjing.east.cloud.dao.PaySpecification;
import com.nanjing.east.cloud.domain.PayHardware;
import com.nanjing.east.cloud.domain.PayOrder;
import com.nanjing.east.cloud.service.OrderService;
import com.nanjing.east.cloud.vo.PayOrder2VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private PayOrderDAO payOrderDAO;

    @Autowired
    private PayHardwareDAO payHardwareDAO;

    @Autowired
    private PaySpecification paySpecification;

    @Override
    public int createOrder(PayOrder payOrder) {
        PayOrder order = payOrderDAO.save(payOrder);
        return order == null ? 0 : 1;
    }

    @Override
    public void updateOrder(PayOrder payOrder, PayOrder newPayOrder) {
        payOrder.setOuterOrderNo(newPayOrder.getOuterOrderNo());
        payOrder.setPayResult(newPayOrder.getPayResult());
        payOrder.setPayResponseMsg(newPayOrder.getPayResponseMsg());
        payOrderDAO.save(payOrder);
    }

    @Override
    public PayOrder queryOrderByNo(String orderNo) {
        PayOrder payOrder = payOrderDAO.findByPayOrderNo(orderNo);
        return payOrder;
    }


    /**
     * 1、查询支付成功 锁未开的  5分钟之前的数据
     * 2、默认开锁成功
     *
     * @return
     */
    @Override
    public List<PayOrder2VO> queryOkOrder(int minute, String storeId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        long time = minute * 60 * 1000;//5分钟
        Date beforeDate = new Date(now.getTime() - time);
        List<PayOrder> payResult = paySpecification.search(beforeDate, 2, storeId);
        List<PayOrder2VO> payOrderVOS = new ArrayList<>();
        if (payResult != null && payResult.size() > 0) {
            payResult.stream().forEach(e -> {
                PayHardware payHardware = payHardwareDAO.findByPayOrderNoAndLockStatus(e.getPayOrderNo(), "0");
                if (payHardware != null) {
                    PayOrder2VO payOrderVO = PayOrder2VO.builder()
                            .payOrderNo(e.getPayOrderNo())
                            .storeId(payHardware.getStoreId())
                            .cabinetId(payHardware.getCabinetId())
                            .lockId(payHardware.getLockId())
                            .payAmount(e.getPayAmount())
                            .build();
                    payOrderVOS.add(payOrderVO);
                }

            });
        }
        return (payOrderVOS.size() >= 2) ? payOrderVOS.subList(0, 2) : payOrderVOS;
    }

    @Override
    public List<PayOrder> queryAllOrder(Integer payChannelId, Integer result) {
        List<PayOrder> payResult = payOrderDAO.findByPayResultAndPayChannelId(result, payChannelId);
        return payResult;
    }


}
